//
//  WebViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/1.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "WebViewController.h"
#import "Utility.h"
#import "Constants.h"
#import <MobClick.h>
@interface WebViewController ()

@end

@implementation WebViewController
{
    __weak IBOutlet UIWebView *webView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"webview"];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"webview"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
    [self configureNavigationBar];
    if (_destinationUrl)
    {
        NSURL *url = [NSURL URLWithString:_destinationUrl];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [webView loadRequest:request];
        [self.view addSubview:webView];
    }
    if (_outTradeNo)
    {
        NSString *urlString = [NSString stringWithFormat:@"http://api.fangmm.com/v3/tradewap/alipay?app=user&outTradeNo=%@",_outTradeNo];
        NSURL *url = [NSURL URLWithString:urlString];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
        request.HTTPMethod = @"GET";
        [request addValue:[NSString stringWithFormat:@"bearer %@",[Utility getObjectForKey:kToken]]forHTTPHeaderField:@"Authorization"];
        NSLog(@"%@",request.allHTTPHeaderFields);
        UIWebView *webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        [webView loadRequest:request];
        [self.view addSubview:webView];
    }
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)shouldAutorotate {
    return NO;
}

//- (void)viewWillAppear:(BOOL)animated {
//    [Utility setObject:@"true" key:kCanBeLandScape];
//
//    [[UIApplication sharedApplication]setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
//    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
//        NSNumber *a = [[NSNumber alloc]initWithInt:4];
//        [[UIDevice currentDevice] performSelector:@selector(setOrientation:)withObject:a];
//    }
//
////    [[UIApplication sharedApplication] ];
//
//}
//
//- (void)viewWillDisappear:(BOOL)animated {
//    [Utility setObject:@"false" key:kCanBeLandScape];
//    [[UIApplication sharedApplication]setStatusBarOrientation:UIInterfaceOrientationPortrait];
//    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
//        NSNumber *a = [[NSNumber alloc]initWithInt:1];
//        [[UIDevice currentDevice] performSelector:@selector(setOrientation:)withObject:a];
//    }
//
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)configureNavigationBar
{
    self.title = self.titleString;
    self.navigationItem.hidesBackButton = true;
    UIImage *backImage = [[UIImage imageNamed:@"back-18-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:@selector(clickToPopBack)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

- (void)clickToPopBack {
    [self.navigationController popViewControllerAnimated:true];
}
@end
